<?php

require( 'util.php' );

validate_action_get( array( 'pid' ) );
$pid = $_GET['pid'];

require( 'db.php' );

$sql = "SELECT user.name, m_id, subject, message, dateline, comment_times, access_times FROM page, user WHERE p_id = $pid AND page.u_id = user.u_id";
$result = mysql_query( $sql ) or report_err_and_exit( mysql_error() );
$thread = mysql_fetch_array( $result ) or report_err_and_exit( '指定帖子不存在。' );

$mid = $thread['m_id'];
$model = get_model( $mid );

// 更新查看次数
$sql = "UPDATE `page` SET `access_times` =  `access_times` + 1 WHERE p_id = $pid";
$result = mysql_query( $sql ) or report_err_and_exit( mysql_error() );

// TDOO:
$page = 1;

////////////////////////////////////////////////////////////

function output_page_control_bar()
{
	global $mid, $page;
	echo <<<ROD
<div id="pgt" class="pgs mbm cl ">
<div class="pgt"></div>
<span class="y pgb"><a href="list.php?&mid=$mid&page=$page">返回列表</a></span>
<a id="newspecial" title="发新帖" href="new_page.php?mid=$mid&mode=write"><img src="static/image/common/pn_post.png" alt="发新帖" /></a><a id="post_reply" href="javascript:;" title="回复"><img src="static/image/common/pn_reply.png" alt="回复" /></a>
</div>
ROD;
}

function output_comment($comment)
{
	$user_space_url = 'javascript:;';
	echo <<<ROD
<div><table cellspacing="0" cellpadding="0">
<tr>
<td class="pls" rowspan="2">

<div class="pi">
<div class="authi">
<a href="$user_space_url" target="_blank" class="xw1">{$comment['uname']}</a>
</div>
</div>

<div class="p_pop blk bui" style="display: none; margin-top: -11px;">
<div class="m z">
</div>
<div class="i y">
<div>
<strong><a href="$user_space_url" target="_blank" class="xi2">{$comment['uname']}</a></strong>
</div>

</div>
</div>

<div class="avatar"><a href="$user_space_url" target="_blank"><img src="http://bbs.1205.cn/icenter/avatar.php?uid=78149&size=middle" /></a></div>

<dl class="pil cl">
	<dt>UID</dt><dd>{$comment['uid']}</dd>
	<dt>注册时间</dt><dd>{$comment['uregtime']}</dd>
</dl>

</td>
<td class="plc">
<div class="pi">


<div class="pti">
<div class="pdbt">
</div>
<div class="authi">
<img class="authicn vm" src="static/image/common/online_member.gif" />
<em>发表于 {$comment['dateline']}</em>

</div>
</div>
</div><div class="pct"><style type="text/css">.pcb{margin-right:0}</style><div class="pcb">
<div class="t_fsz">
<table cellspacing="0" cellpadding="0"><tr><td class="t_f">

{$comment['message']}

</td></tr></table>
</div>
<div id="comment_612998" class="cm">
</div>
<div id="post_rate_div_612998"></div>
</div></div>

</td></tr>
<tr><td class="plc plm"></td></tr>
<tr>
<td class="pls"></td>
<td class="plc">
<div class="po">
<div class="pob cl">

<em>
<a class="fastre" href="forum.php" onclick="showWindow('reply', this.href)">回复</a>
</em>


</div>
</div>

</td>
</tr>
<tr class="ad">
<td class="pls"></td>
<td class="plc"></td>
</tr>
</table>
</div>
ROD;
}

//********************** 页面内容开始 **********************

require_once( 'page_frame.inc.php' );

output_frame_header( $thread['subject'] );

?>

<script type="text/javascript" src="jquery.js"></script>
<script charset="utf-8" src="kindeditor/kindeditor-min.js"></script>
<script charset="utf-8" src="kindeditor/lang/zh_CN.js"></script>

<script>

    editor = null;
    $(function() {
        editor = KindEditor.create('#msg');
    });
    
</script>

<div style="height=14px">&nbsp</div>

<link rel="stylesheet" type="text/css" href="static/style/common.css" />
<link rel="stylesheet" type="text/css" href="static/style/forum_viewthread.css" />

<?php output_page_control_bar(); ?>

<div id="postlist" class="pl bm">

<table cellspacing="0" cellpadding="0">
	<tr>
		<td class="pls ptm pbm">
			<div class="hm">
			<span class="xg1">查看:</span> <span class="xi1"><?php echo $thread['access_times']; ?></span><span class="pipe">|</span><span class="xg1">回复:</span> <span class="xi1"><?php echo $thread['comment_times']; ?></span>
			</div>
		</td>
		<td class="plc ptm pbn">
			<h1 class="ts">
			<a href="view_page.php?pid=<?php echo $pid; ?>" id="thread_subject">
			<?php echo $thread['subject']; ?>
			</a>
			</h1>
		</td>
	</tr>
</table>

<!-- 一行蓝色条 -->
<table cellspacing="0" cellpadding="0" class="ad"><tr><td class="pls"></td><td class="plc"></td></tr></table>

<?php

echo "<a href='list.php?mid=$mid'>", $model['topic'], '</a> &gt; ';
echo "<a href='view_page.php?pid=$pid'>", $thread['subject'], '</a><br /><br />', "\n";

echo "{$thread['name']} (楼主) 在 {$thread['dateline']} 时写道：<br />";

// KindEditor 自动转义？
$msg = str_replace( '\"', '"', $thread['message'] );
$msg = str_replace( "\'", '"', $thread['message'] );
echo $msg;

// 输出所有回复
$sql = "SELECT user.u_id as uid, user.name as uname, user.birthday as uregtime, comment.message, comment.dateline FROM user, comment WHERE comment.p_id = $pid AND user.u_id = comment.u_id";
$result = mysql_query( $sql ) or report_err_and_exit( mysql_error() );
while ($comment = mysql_fetch_array( $result ))
{
    $comment['message'] = str_replace( '\"', '"', $comment['message'] );
    $comment['message'] = str_replace( "\'", "'", $comment['message'] );
	
	output_comment( $comment );
}

?>

<?php output_page_control_bar(); ?>

<!--
<?php

echo "<a href='list.php?mid=$mid'>", $model['topic'], '</a> &gt; ';
echo "<a href='view_page.php?pid=$pid'>", $thread['subject'], '</a><br /><br />', "\n";

echo "{$thread['name']} (楼主) 在 {$thread['dateline']} 时写道：<br />";

// KindEditor 自动转义？
$msg = str_replace( '\"', '"', $thread['message'] );
$msg = str_replace( "\'", '"', $thread['message'] );
echo $msg;

// 输出所有回复
$sql = "SELECT user.name, comment.message, comment.dateline FROM user, comment WHERE comment.p_id = $pid AND user.u_id = comment.u_id";
$result = mysql_query( $sql ) or report_err_and_exit( mysql_error() );
while (list( $uname, $comment, $comment_time ) = mysql_fetch_array( $result ))
{
	echo '<hr />', $uname, ' 在 ', $comment_time, ' 时写道：<br />';
    $comment = str_replace( '\"', '"', $comment );
    $comment = str_replace( "\'", "'", $comment );
	echo $comment;
}

?>-->

<br /><br /><hr />
评论帖子：<br />
<form id="commentEditor" method="POST" action="comment.php">
    <textarea id="msg" name="msg" rows="5" cols="160">强烈支持！</textarea>
    <p><input type="submit" value="Comment" /></p>
    <input type="hidden" name="pid" value="<?php echo $pid; ?>" />
</form>

<script>

    $("#commentEditor").submit( function()
    {
        var msg = editor.html();
        var empty = (!msg || msg == "");
        
        if (!empty)
        {
            editor.sync();
            return true;
        }
        else
        {
            alert('评论内容不能为空。');
            editor.focus();
            
            return false;
        }
    });

</script>

<?php

//********************** 页面内容结束 **********************

output_frame_footer();

?>